package kr.hkit.androidcomplete03.ch22;

import java.util.Random;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;

public class Ball {
	int x, y;
	int rad;
	int dx, dy;
	int color;
	int count;
	Paint paint;
	
	static Ball creat(int x, int y){
		Random rnd = new Random();
		Ball newBall = new Ball();
		newBall.paint = new Paint(Paint.ANTI_ALIAS_FLAG);
		newBall.x = x;
		newBall.y = y;
		newBall.rad = rnd.nextInt(50);
		do{
			newBall.dx = rnd.nextInt(11) - 5;
			newBall.dy = rnd.nextInt(11) - 5;
		}while(newBall.dx == 0 || newBall.dy == 0);
		
		newBall.count = 0;
		newBall.color = Color.rgb(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
		return newBall;
	}
	
	void move(int width, int height){
		x += dx;
		y += dy;
		
		if(x < rad || x > width - rad){
			dx = dx * -1;
			count++;
		}
		if(y < rad || y > height - rad){
			dy = dy * -1;
			count++;
		}
	}
	
	void draw(Canvas canvas){
		
		for(int r = rad, alpha = 1; r > 4; r--, alpha += 5){
			paint.setColor(Color.argb(alpha, Color.red(color), Color.green(color), Color.blue(color)));
			canvas.drawCircle(x, y, r, paint);
		}
	}
}
